package com.fanqiechaodan.prototype.copy.deep;

import com.alibaba.fastjson.JSON;
import org.springframework.util.SerializationUtils;

import java.io.IOException;

/**
 * @author fanqiechaodan
 * @Classname Demo
 * @Description 深拷贝
 */
public class Demo {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person person1 = new Person("张三", 18, new Address("上海", "南京路"));
        // 重写clone完成深拷贝
        Person person2 = (Person) person1.clone();
        // 使用工具类完成深拷贝
        Person person3 = (Person) SerializationUtils.deserialize(SerializationUtils.serialize(person1));
        System.out.println(JSON.toJSONString(person1));
        System.out.println(JSON.toJSONString(person2));
        System.out.println(JSON.toJSONString(person3));
        System.out.println("深拷贝后:");

        person1.getAddress().setCity("南京");
        System.out.println(JSON.toJSONString(person1));
        System.out.println(JSON.toJSONString(person2));
        System.out.println(JSON.toJSONString(person3));
    }
}
